package 二叉树;


/**
 * https://leetcode.cn/problems/flatten-binary-tree-to-linked-list/
 */
public class _114_二叉树展开为链表 {

    public void flatten(TreeNode root) {
        if (root == null) return;

        // 后序遍历，先left，再right
        flatten(root.left);
        flatten(root.right);

        TreeNode left = root.left;
        TreeNode right = root.right;

        root.left = null;
        root.right = left;

        TreeNode node = root;

        while (node.right != null) {
            node = node.right;
        }

        node.right = right;
    }
}
